'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN', 'shiftform', 'validate', 'validate.en', 'ajaxTab'], function($) {
  
  return {
    init: function() {
      this.initPage();
      this.infoEdit();
      this.profileEdit();
      this.wechatEdit();
      this.fitEdit();
      this.targetDataEdit();
      this.bwhEdit();
      this.targetEdit();
    },

    initPage: function(){
      $(".ajax-tab").ajaxtab();
    },

    infoEdit: function(status) {
      $(document).on('shown.bs.modal', '#info-edit', function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form");                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      })
    },

    profileEdit: function(status) {
      $(document).on('shown.bs.modal', '#profile-edit', function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form");                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      })
    },

    wechatEdit: function(status) {
      $(document).on('shown.bs.modal', '#wechat-edit', function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form");                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          },
          success: "#wechat-ajax:load:refresh"
        });
      })
    },

    fitEdit: function(status) {
      $(document).on('shown.bs.modal', '#fit-edit', function(e){
        $('select[name="gender"]').select2({
          minimumResultsForSearch : -1,
          allowClear : true,
        });

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form");                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          },
          success: "#fit-ajax:load:refresh"
        })
      })
    },

    targetDataEdit: function(status) {
      $(document).on('shown.bs.modal', '#target-data-edit', function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form");                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          },
          success: "#target-ajax:load:refresh"
        })
      })
    },

    bwhEdit: function() {
      $(document).on('shown.bs.modal', '#bwh-edit', function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form");                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          },
          success: "#body-ajax:load:refresh"
        });
      });
    },
    
    targetEdit: function() {
      $(document).on('shown.bs.modal', '#target-edit', function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form");                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          },
          success: "#body-ajax:load:refresh"
        });
      });
    }
  }
})
